//
//  JWApiExtension.m
//  moduleApiExtension
//
//  Created by 晟骏 王 on 2021/12/31.
//

#import "JWApiExtension.h"
#import "NSDictionaryUtils.h"

@implementation JWApiExtension

- (id)initWithUZWebView:(UZWebView *)webView {
    if (self = [super initWithUZWebView:webView]) {
        // 初始化方法
    }
    return self;
}

/**
 Save Integer
 */
JS_METHOD(jSetIntegerPrefs:(UZModuleMethodContext *)context) {
	NSDictionary *param = context.param;
	NSString *key = [param stringValueForKey:@"key" defaultValue:@"key"];

	if ([@"incNo" isEqualToString:key]) return;

	NSInteger valueInt = [param integerValueForKey:@"value" defaultValue:NSIntegerMin];
	if (NSIntegerMin != valueInt) {
		[[NSUserDefaults standardUserDefaults] setInteger:valueInt forKey:key];
	}
}

/**
 Save Bool
 */
JS_METHOD(jSetBoolPrefs:(UZModuleMethodContext *)context) {
	NSDictionary *param = context.param;
	NSString *key = [param stringValueForKey:@"key" defaultValue:@"key"];

	NSString *valueBoolStr = [param stringValueForKey:@"value" defaultValue:@""];
	if ([@"" isEqualToString:valueBoolStr]) {
		return;
	}
	if ([@"YES" isEqualToString:valueBoolStr]) {
		[[NSUserDefaults standardUserDefaults] setBool:YES forKey:key];
	} else if ([@"NO" isEqualToString:valueBoolStr]) {
		[[NSUserDefaults standardUserDefaults] setBool:NO forKey:key];
	}
}

/**
 get integer
 */
JS_METHOD_SYNC(jGetIntegerPrefs:(UZModuleMethodContext *)context) {
	NSDictionary *param = context.param;
	NSString *key = [param stringValueForKey:@"key" defaultValue:@"key"];
	return @([[NSUserDefaults standardUserDefaults] integerForKey:key]);
}

/**
 get Bool
 */
JS_METHOD_SYNC(jGetBoolPrefs:(UZModuleMethodContext *)context) {
	NSDictionary *param = context.param;
	NSString *key = [param stringValueForKey:@"key" defaultValue:@"key"];
	return @([[NSUserDefaults standardUserDefaults] boolForKey:key]);
}

/**
 get fix version NO
 */
JS_METHOD_SYNC(jGetIncNo:(UZModuleMethodContext *)context) {
	return @([[NSUserDefaults standardUserDefaults] integerForKey:@"incNo"] ?: -1);
}

@end
